package geektime.spring.springbucks.controller;

import geektime.spring.springbucks.controller.request.CoffeeRequest;
import geektime.spring.springbucks.model.Coffee;
import geektime.spring.springbucks.service.CoffeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/coffee")
@Slf4j
public class CoffeeController {
    @Autowired
    private CoffeeService coffeeService;

    @GetMapping(path = "/", params = "!name")
    @ResponseBody
    public List<Coffee> getAll() {
        List<Coffee> coffee= coffeeService.getAllCoffee();

        log.info("getAllCoffee {}:", coffee);

        return coffee;
    }

    @RequestMapping(path = "/{id}", method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Coffee getById(@PathVariable Long id) {
        Coffee coffee = coffeeService.findCoffeeById(String.valueOf(id));
        log.info("getCoffeeById {}:", coffee);
        return coffee;
    }

    @GetMapping(path = "/", params = "name")
    @ResponseBody
    public Coffee getByName(@RequestParam String name) {
        Coffee coffee = coffeeService.findCoffeeByName(name);
        log.info("getCoffeeByName {}:", coffee);
        return coffee;
    }
}
